package com.act.moudles.user.controller;


import com.act.constant.UserTypeEnums;
import com.act.moudles.user.entity.User;
import com.act.moudles.user.service.ComboService;
import com.act.moudles.user.service.UserService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.act.utils.validations.AddGroup;
import com.act.utils.validations.UpdateGroup;
import com.act.utils.validations.ValidResult;
import com.act.utils.validations.ValidationUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Api(tags = "用户模块")
@RequestMapping("user/user")
@Slf4j
public class UserController {


    @Resource
    private UserService userService;

    @Resource
    private ComboService comboService;


    @GetMapping("findUserTypes")
    @ApiOperation("查询用户类型")
    public DataResult findUserTypes(){
        List<Map<String,Object>> retList = new ArrayList<>();
        for(UserTypeEnums e : UserTypeEnums.values()){
            Map<String,Object> map = new HashMap<>();
            map.put("dictVaue",e.getUserType());
            map.put("dictLabel",e.getUserTypeName());
            retList.add(map);
        }
        return DataResult.success("查询成功！",retList);
    }


    @PostMapping("page")
    @ApiOperation("用户信息分页查询")
    public DataResult page(@RequestBody PageSearch<User> pageSearch) {
        log.info("分页参数：{}", JSON.toJSONString(pageSearch));
        Page<User> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("查询成功！",userService.pageUser(page,pageSearch.getDomain()));
    }


    @PostMapping("saveUser")
    @ApiOperation("新增用户信息")
    public DataResult saveUser(@RequestBody User user){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(user, AddGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        //新增
        String addRes = userService.addUser(user);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("新增成功");
    }


    @PostMapping("getComByUserType")
    @ApiOperation("获取到用户默认套餐信息")
    public DataResult  getComByUserType(@RequestBody User user){
        Long comboId = userService.findComboIdByOrgOrUserId(user.getOrgId());
        if(comboId == null ||  user.getUserType() - UserTypeEnums.PERSON.getUserType() == 0){
            return DataResult.success("查询成功！",comboService.list());
        }
        return DataResult.success("查询单个成功", Arrays.asList(comboService.getById(comboId)));
    }


    @PostMapping("modifyUser")
    @ApiOperation("修改用户信息")
    public DataResult modifyUser(@RequestBody User user){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(user, UpdateGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        //修改
        String updRes = userService.updateUser(user);
        if(StringUtils.isNotEmpty(updRes)){
            return DataResult.fail(updRes);
        }
        return DataResult.success("修改成功");
    }

    @GetMapping("deleteFalse")
    @ApiOperation("逻辑删除用户")
    public DataResult deleteFalse(Long id){
        log.info("逻辑删除：{}", id);
        if(id == null){
            return DataResult.fail("主键不能未空");
        }
        User user = new User();
        user.setDelFlag(0);
        user.setUserId(id);
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.eq(User::getUserId,id);
        userService.update(user,query);
        return DataResult.success("删除完成");
    }


}
